<?php
class Model{

	private $db = NULL;
	private $opts = array();
	private $error = false;
	private $table = NULL;
	public $dbPrefix = '';

	public function __construct($table=''){
		global $CONFIGS;
		if( isset($CONFIGS['dbPrefix']) ) $this->dbPrefix = $CONFIGS['dbPrefix'];
		$this->db = DB::init($CONFIGS);
		$this->get_table($table);
	}

	public function __call($method, $args){
		$arrOpts = array('field','where','value','order','limit','group','table');
		if( in_array($method, $arrOpts) ){
			if( $method=='table' && $this->dbPrefix ){
				$this->opts[$method] = $this->dbPrefix.$args[0];
			}else{
				$this->opts[$method] = $args[0];
			}
		}
		return $this;
	}

	private function parseOpts(){
		$return = array();
		if( is_string($this->opts['field']) ){
			$return['field'] = $this->opts['field'];
		} elseif ( is_array($this->opts['field']) ) {
			$field = implode($this->opts['field'],',');
			$return['field'] = '`'.str_replace(',',"`,`",$field).'`';
		}else{
			$return['field'] = '*';
		}
		unset($this->opts['field']);

		if( $this->opts['where'] && is_string($this->opts['where']) ){
			$return['where'] = $this->opts['where'];
		} else {
			$return['where'] = '1';
		}
		unset($this->opts['where']);
		
		if( $this->opts['value'] && is_array($this->opts['value']) ){
			$set = '';
			foreach($this->opts['value'] as $k=>$v){
				$v = $v===NULL ? 'NULL' : "'".$v."'";
				$set .= "`$k`=".$v.",";
			}
			$return['value'] = trim($set,',');
		} else {
			$return['value'] = $this->opts['value'];
		}	
		unset($this->opts['value']);

		if( $this->opts['order'] && is_string($this->opts['order']) ){
			$return['order'] = $this->opts['order'];
			unset($this->opts['order']);
		}

		if( $this->opts['limit'] && is_string($this->opts['limit']) ){
			$return['limit'] = $this->opts['limit'];
			unset($this->opts['limit']);
		}

		if( $this->opts['group'] && is_string($this->opts['group']) ){
			$return['group'] = $this->opts['group'];
			unset($this->opts['group']);
		}

		return $return;
	}

	private function get_table( $tableName ){
		if( !empty($tableName) ){
			$this->opts['table'] = empty($this->dbPrefix) ? $tableName : $this->dbPrefix.$tableName;	
		}
	}

	private function get_view( $viewName ){
	
	}

	public function query( $sql="" ){
		if( empty($sql)	){
			$sql = $this->sql;
		}else{
			$this->sql = $sql;
		}
		return $this->db->query( $this->sql );
	}

	public function get(){
		$opts = $this->parseOpts();	
		$this->sql = 'SELECT '.$opts['field'].' FROM '.$this->opts['table'].' WHERE '.$opts['where'];
		if( isset($opts['group']) ){
			$this->sql .= ' GROUP BY '.$opts['group'];
		}
		if( isset($opts['order']) ){
			$this->sql .= ' ORDER BY '.$opts['order'];
		}
		if( isset($opts['limit']) ){
			$this->sql .= ' LIMIT '.$opts['limit'];
		}
		$rs = $this->query();
		if( !empty($rs) ){
			$arr = array();
			while( $row = $this->db->fetch($rs,1) ){
				$arr[] = $row;
			}
			return $arr;
		}else{
			return NULL;
		}
	}

	public function get_one(){
		$opts = $this->parseOpts();	
		$this->sql = 'SELECT '.$opts['field'].' FROM `'.$this->opts['table'].'` WHERE '.$opts['where'];
		if( isset($opts['group']) ){
			$this->sql .= ' GROUP BY '.$opts['group'];
		}
		if( isset($opts['order']) ){
			$this->sql .= ' ORDER BY '.$opts['order'];
		}
		$this->sql .= ' LIMIT 0,1';
		$rs = $this->query();
		if( !empty($rs) ){
			return $this->db->fetch($rs,1);
		}else{
			return NULL;
		}
	}

	public function add(){
		$opts = $this->parseOpts();
		$this->sql = 'INSERT INTO `'.$this->opts['table'].'` SET '.$opts['value'];
		$rs = $this->query();
		if( $rs ){
			return $this->db->insert_id();
		}else{
			return $rs;
		}
	}

	public function del(){
		$opts = $this->parseOpts();
		if( !isset($opts['where']) ){
			// 无条件的删除操作时返回
			return -1;
		}
		$this->sql = 'DELETE FROM `'.$this->opts['table'].'` WHERE '.$opts['where'];
		if( isset($opts['group']) ){
			$this->sql .= ' GROUP BY '.$opts['group'];
		}
		if( isset($opts['order']) ){
			$this->sql .= ' ORDER BY '.$opts['order'];
		}
		if( isset($opts['limit']) ){
			$this->sql .= ' LIMIT '.$opts['limit'];
		}
		$rs = $this->query();
		if( !empty($rs) ){
			return $this->db->affected_rows();
		}else{
			return NULL;
		}
	}

	public function update(){
		$opts = $this->parseOpts();
		$this->sql = 'UPDATE `'.$this->opts['table'].'` SET '.$opts['value'].' WHERE '.$opts['where'];
		if( isset($opts['group']) ){
			$this->sql .= ' GROUP BY '.$opts['group'];
		}
		if( isset($opts['order']) ){
			$this->sql .= ' ORDER BY '.$opts['order'];
		}
		if( isset($opts['limit']) ){
			$this->sql .= ' LIMIT '.$opts['limit'];
		}
		$rs = $this->query();
		if( $rs ){
			return $this->db->affected_rows();
		}else{
			return $rs;
		}
	}

	public function get_columns(){
		if( empty($this->opts['table']) ){
			return NULL;
		}
		$this->sql = 'SHOW COLUMNS FROM '.$this->opts['table'];
		$rs = $this->query();
		while( $row = $this->db->fetch($rs) ){
			$arr[] = $row['Field'];	
		}
		return $arr;
	}

	public function get_sql(){
		return $this->db->get_sql();
	}

	public function get_error(){
		return $this->db->error();
	}

}
